// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using BlueOnionSoftware.Bloget.Properties;

namespace BlueOnionSoftware.Bloget
{
    internal class SearchView : View
    {
        internal SearchView(Bloget bloget)
            : base(bloget)
        {
        }

        protected override string RootClassId()
        {
            return "search-view";
        }

        protected override void ShowView()
        {
            var posts = Search.Find(Bloget, Bloget.QuerySort);

            if (Bloget.SearchResultsTemplate == null)
            {
                NormalView(posts);
            }

            else
            {
                CustomView(posts);
            }
        }

        private HyperLink Title(Post post)
        {
            if (post == null)
            {
                throw new ArgumentNullException("post");
            }
            var link = new HyperLink
            {
                Text = post.Title,
                NavigateUrl = Bloget.BuildQueryString(Mode.Post, post, 0, null, null, default(DateTime),
                                                      null)
            };
            return link;
        }

        private static readonly Regex RemoveHtml = new Regex("<(.|\n)*?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        internal static string Excerpt(Bloget bloget, Post post, int length)
        {
            if (bloget == null)
            {
                throw new ArgumentNullException("bloget");
            }
            if (post == null)
            {
                throw new ArgumentNullException("post");
            }
            var content = bloget.Blog.LoadContent(post.Id);
            var plainText = RemoveHtml.Replace(content.Text, string.Empty);
            return plainText.Substring(0, Math.Min(plainText.Length, Math.Min(content.Text.Length, Math.Max(10, length))));
        }

        private void NormalView(Post[] posts)
        {
            if (posts == null)
            {
                throw new ArgumentNullException("posts");
            }
            var results = string.Format(CultureInfo.CurrentCulture,
                                        Resources.SearchResults, posts.Length, Bloget.QuerySort);

            Bloget.WriteLine("<h2>" + results + "</h2>");

            foreach (var post in posts)
            {
                Bloget.Write("<h3>");
                Bloget.Controls.Add(Title(post));
                Bloget.WriteLine("</h3>");
                Bloget.WriteLine("<div>" + Excerpt(Bloget, post, 250) + "...</div>");
            }
        }

        private void CustomView(IEnumerable<Post> posts)
        {
            if (posts == null)
            {
                throw new ArgumentNullException("posts");
            }
            foreach (var post in posts)
            {
                var content = Bloget.Blog.LoadContent(post.Id);
                var postContainer = new PostContainerControl(Bloget, Bloget.Blog, post, content);
                Bloget.SearchResultsTemplate.InstantiateIn(postContainer);
                Bloget.Controls.Add(postContainer);
            }
        }
    }
}